package com.usian.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult follow(UserRelationDto dto) {

        //1:校验参数
        if(dto.getOperation()<0 ||dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:远程调用获取作者信息
        ApAuthor apAuthor = apAuthorFeign.findById(dto.getAuthorId());
        if(apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AUTHOR_DATA_EXIST);
        }
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        //3:操作关注 0和取消关注1
        if(dto.getOperation()==0){
            //关注
            //查看关注的用户是否存在
            ApUser apUser1 = apUserMapper.selectById(apAuthor.getUserId());
            if(apUser1==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"关注用户不存在");
            }

            //ap_user_follow 录入数据
            List<ApUserFollow> followList = apUserFollowMapper.findByUserIdAndFollowId(apUser.getId(), dto.getAuthorId());
            if(followList.size()==0){
                //保存app用户关注信息
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setUserId(apUser.getId());
                apUserFollow.setFollowId(dto.getAuthorId());
                apUserFollow.setFollowName(apAuthor.getName());
                apUserFollow.setCreatedTime(new Date());
                apUserFollow.setIsNotice(true);
                apUserFollow.setLevel((short)1);
                apUserFollowMapper.insert(apUserFollow);

                //保存粉丝表  为了去重 先查询再保存
                List<ApUserFan> userFanList = apUserFanMapper.findByUserIdAndFansId(dto.getAuthorId(), apUser.getId());
                if(userFanList.size()==0){
                    ApUserFan apUserFan = new ApUserFan();
                    apUserFan.setUserId(dto.getAuthorId());
                    apUserFan.setFansId(apUser.getId().longValue());
                    apUserFan.setFansName(apUser1.getName());
                    apUserFan.setLevel((short)0);
                    apUserFan.setIsDisplay(true);
                    apUserFan.setIsShieldLetter(false);
                    apUserFan.setIsShieldComment(false);
                    apUserFan.setCreatedTime(new Date());
                    apUserFanMapper.insert(apUserFan);
                }

                //关注成功后发送发注消息，在行为服务中获取后保存用户关注行为记录
                FollowBehaviorDto dto1 =new FollowBehaviorDto();
                dto1.setArticleId(dto.getArticleId());
                dto1.setFollowId(dto.getAuthorId());
                dto1.setUserId(apUser.getId());
                kafkaTemplate.send("followTopic", JSON.toJSONString(dto1));

            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"作者已关注");
            }
        }else if(dto.getOperation()==1){
            //取消关注  删除 ap_user_follow    ap_user_fans  先查 有 删除  没有直接过
            List<ApUserFollow> followList = apUserFollowMapper.findByUserIdAndFollowId(apUser.getId(), dto.getAuthorId());
            if(followList.size()>0){
                apUserFollowMapper.deleteByUserIdAndFollowId(apUser.getId(),dto.getAuthorId());
            }
            List<ApUserFan> userFanList = apUserFanMapper.findByUserIdAndFansId(dto.getAuthorId(), apUser.getId());
            if(userFanList.size()>0){
                apUserFanMapper.deleteByUserIdAndFansId(dto.getAuthorId(), apUser.getId());
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
